#include "jobgovenor.h"

JobGovenor::JobGovenor():Process()
{
    name="JobGovenor";
    haveMemory = new intListBase();
}
JobGovenor::~JobGovenor()
{
}
void JobGovenor::Run()
{
    LogManager::getSingleton().Message("JobGovernor", "step "+LogManager::getSingleton().toStr(currentStep));
    switch(currentStep){
    case 0:{
        ResourceAllocator::getSingleton().askForResource("UserMemory",id);
        currentStep++;
    }
    break;
    case 1:{
        Resource* nx = usingResourceList.at(usingResourceList.size()-1);
        ElementsBase* temp = nx->getElement();
        int blockNr;
        int converted;
        bool lyginisElementas=true;
        for (ElementsIterator it = temp->begin(); it != temp->end(); ++it){
            if (haveMemory->size() < 17) {
                if (lyginisElementas) {
                   std::stringstream(*it)>> converted;
                   blockNr = converted;
                   lyginisElementas=false;
             } else {
                    if ((*it )=="t") {
                        std::replace(it,it+1,"t","f");
                        haveMemory->push_back(blockNr);
                    }
                 lyginisElementas=true;
                };
             };

        };
        if (haveMemory->size() == 17) {
            ResourceAllocator::getSingleton().addResource(new MessageToLoader(id));
            ResourceAllocator::getSingleton().freeResource(nx);
            currentStep++;
        } else {
            currentStep=1;
        }
    }
    break;
    case 2:{
        ResourceAllocator::getSingleton().askForResource("LoadedProgram",id);
        currentStep++;
    }
    break;
    case 3:{
        ResourceAllocator::getSingleton().destroyResource(usingResourceList.at(usingResourceList.size()-1));
        sharedMemoryControlId = Planner::getSingleton().createProcess(id,READY,50, new SharedMemoryControl());
        currentStep++;
    }
    break;
    case 4:{
        //virtualMachineId = Planner::getSingleton().createProcess(id,READY,40, new VirtualMachine(puslapiu lentele));
        currentStep++;//truksta puslapiu lenteles
    }
    break;
    case 5:{
        ResourceAllocator::getSingleton().askForResource("InteruptResource",id);
        currentStep++;
    }
    case 6:{
        Planner::getSingleton().stopProcess(virtualMachineId);
        currentStep++;
    }
    break;
    case 7:{
/*    interuptToExecute = reinterpret_cast<Interupt*>(*usingResourceList.end());
    if(interuptToExecute.IO!=0)
    {
        if(interuptToExecute.IO>0)
            {currentStep=8;}
            else{currentStep=10;}
    }
    else
    {
    if(interuptToExecute.SharedMemory!=0)
        {currentStep=13;}
        else
        {
            if(interuptToExecute.Timer=0)
                {currentStep=18;}
                else{currentStep=16;}
        }
    }
*/
    }
    break;
    case 8:{
        lineToWrite = reinterpret_cast<LineForOutput*>(ResourceAllocator::getSingleton().getResource("LineForOutput"));
        lineToWrite->out = "Cia ka nori isvest parasai";
        currentStep++;
        //Nezinau ar gera vieta
    }
    break;
    case 9:{
        ResourceAllocator::getSingleton().askForResource("LineOutputExecuted",id);
        //currentStep=18;
    }
    break;
    case 10:{
        ResourceAllocator::getSingleton().addResource(new MessageToGet(id));
        currentStep++;
    }
    break;
    case 11:{
        ResourceAllocator::getSingleton().askForResource("UserInput",id);
        currentStep++;
    }
    break;
    case 12:{
        //reinterpret_cast<UserInput*>(*usingResourceList.end())->input;
        //Kopijuoti virsu I atminti
        currentStep=18;
    }
    break;
    case 13:{
        ResourceAllocator::getSingleton().freeResource(ResourceAllocator::getSingleton().getResource("MessageToSMC"));
        currentStep++;
    }
    break;
    case 14:{
        ResourceAllocator::getSingleton().askForResource("MessageToJobGovenor",id);
        currentStep++;
    }
    break;
    case 15:{
     //   if((reinterpret_cast<MessageToJobGovenor*>(*usingResourceList.end())->parameter)=0)
      //  {currentStep=18;}
      //      else {currentStep++;};
    }
    break;
    case 16:{
        //atlaisvintiAtiminti
        currentStep++;
    }
    break;
    case 17:{
        Planner::getSingleton().deleteProcess(virtualMachineId);
        Planner::getSingleton().deleteProcess(sharedMemoryControlId);
        //Kazkaip pagal id atlaisvint ta  programa buferije kuris ji sukure
        Planner::getSingleton().blockProcess(id);
    }
    break;
    case 18:{
        Planner::getSingleton().activateProcess(virtualMachineId);
        currentStep++;
    }
    break;
    case 19:{
        ResourceAllocator::getSingleton().freeResource(ResourceAllocator::getSingleton().getResource("VirtualMachine"));
        currentStep=5;
    }
    break;
    default:
      qDebug() << "gg JobGovenor";
    }
}
